package com.zhx.sweet.util;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;

import java.util.Map;

public class EntityWapperUtil<T> extends Wrapper<T> {
    protected T entity = null;

    public EntityWapperUtil() {
    }

    public EntityWapperUtil(T entity) {
        this.entity = entity;
    }

    public EntityWapperUtil(T entity, String sqlSelect) {
        this.entity = entity;
        this.sqlSelect = sqlSelect;
    }

    public EntityWapperUtil like(Map<String,Object> map,String value){
        if (map.get(value) != null) {
            this.like(value,"%"+map.get(value).toString()+"%");
            map.remove(value);
        }

        return this;
    }
    public EntityWapperUtil eq(String value,Map<String,Object> map){
        if (map.get(value) != null) {
            this.eq(value,map.get(value).toString());
            map.remove(value);
        }

        return this;
    }

    public EntityWapperUtil isNotNull(boolean condition, String columns) {
    if (condition) {
        this.sql.IS_NOT_NULL(columns);
    }

    return this;
    }
    public EntityWapperUtil isNull(boolean condition, String columns) {
        if (condition) {
            this.sql.IS_NULL(columns);
        }

        return this;
    }
    @Override
    public T getEntity() {
        return this.entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }


    @Override
    public String getSqlSegment() {
        String sqlWhere = this.sql.toString();
        if (StringUtils.isEmpty(sqlWhere)) {
            return null;
        } else {
            return this.isWhere != null ? (this.isWhere ? sqlWhere : sqlWhere.replaceFirst("WHERE", this.AND_OR)) : sqlWhere.replaceFirst("WHERE", this.AND_OR);
        }
    }
}
